package com.shanzmoo.platform.tomember.api.domain;

import cn.hutool.core.util.StrUtil;
import com.shanzmoo.db.module.merchant.entity.MerchantInfoEntity;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;

/**
 * 认证用户
 * @author: Gzhao 2020/11/9
 */
@Data
@Accessors(chain = true)
public class AuthUser implements UserDetails {

    /**
     * 用户id
     */
    private Integer userId;

    /**
     * 账号
     */
    private String mobile;

    /**
     * 密码
     */
    private String password;

    /**
     * 用户状态
     */
    private Boolean status;

    public static AuthUser create(MerchantInfoEntity userEntity) {
        if (null == userEntity){
            return null;
        }
        return  new AuthUser()
                .setUserId(userEntity.getId())
                .setMobile(userEntity.getMobile())
                .setStatus(StrUtil.equals(userEntity.getStatus(),"1"))
                .setPassword(userEntity.getPassword());
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return mobile;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return status;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
